package com.erp.action;

import java.util.Date;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.erp.dto.Page;
import com.erp.dto.Reply;
import com.erp.dto.Topic;
import com.erp.dto.User;

@SuppressWarnings("serial")
@Controller(value = "replyAction")
@Scope(value = "prototype")
public class ReplyAction extends ModelDrivenBaseAction<Reply> {

	/**
	 * 发表新回复页面
	 */
	public String addPage() throws Exception {
		return "addPage";
	}

	/**
	 * 发表新回复操作
	 */
	public void add() throws Exception {
		model.setPostTime(new Date());
		model.setAuthor((User) session.get("user"));
		replyService.add(model);
		writeJson("msg", "true");
	}

	/**
	 * 删除操作
	 */
	public void delete() throws Exception {
		replyService.setStatusDeleted(model);
		writeJson("msg", "true");
	}

	/**
	 * 恢复主题
	 */
	public void undelete() throws Exception {
		replyService.setStatusNormal(model);
		writeJson("msg", "true");
	}

	/**
	 * 彻底删除主题
	 */
	public void completelyDelete() throws Exception {
		replyService.delete(model);
		writeJson("msg", "true");
	}

	/**
	 * 显示回复列表
	 */
	public String list() throws Exception {
		if (page == null) {
			page = new Page();
		}
		return pagedList();
	}

	/**
	 * 显示所以回复列表
	 */
	public String listAll() throws Exception {
		Topic topic = topicService.getTopicById(model.getTopicId());
		if (topic == null) {
			return "404error";
		}
		request.put("topic", topic);
		request.put("replyList", replyService.listByTopicId(model));
		return "list";
	}

	/**
	 * 分页后的用户列表
	 */
	public String pagedList() throws Exception {
		Topic topic = topicService.getTopicById(model.getTopicId());
		if (topic == null) {
			return "404error";
		}
		request.put("topic", topic);
		request.put("replyList", replyService.pagedListByTopicId(model, page));
		pushIntoValueStack(page);
		return "list";
	}

	public String query() throws Exception {

		return "";
	}

	/**
	 * 获取跟帖回收站列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String recycleList() throws Exception {
		request.put("replyRecycleList", replyService.getRecycleList());
		return "recycleList";
	}
}
